﻿using System;
using System.Collections.Generic;
using System.Linq;
using TaleWorlds.CampaignSystem.Actions;
using TaleWorlds.CampaignSystem.Extensions;
using TaleWorlds.CampaignSystem.Roster;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.SaveSystem;

namespace TaleWorlds.CampaignSystem.Party.PartyComponents
{
	// Token: 0x020002B1 RID: 689
	public class CaravanPartyComponent : PartyComponent
	{
		// Token: 0x06002801 RID: 10241 RVA: 0x000AAF41 File Offset: 0x000A9141
		internal static void AutoGeneratedStaticCollectObjectsCaravanPartyComponent(object o, List<object> collectedObjects)
		{
			((CaravanPartyComponent)o).AutoGeneratedInstanceCollectObjects(collectedObjects);
		}

		// Token: 0x06002802 RID: 10242 RVA: 0x000AAF4F File Offset: 0x000A914F
		protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
		{
			base.AutoGeneratedInstanceCollectObjects(collectedObjects);
			collectedObjects.Add(this._leader);
			collectedObjects.Add(this.Settlement);
			collectedObjects.Add(this.Owner);
		}

		// Token: 0x06002803 RID: 10243 RVA: 0x000AAF7C File Offset: 0x000A917C
		internal static object AutoGeneratedGetMemberValueSettlement(object o)
		{
			return ((CaravanPartyComponent)o).Settlement;
		}

		// Token: 0x06002804 RID: 10244 RVA: 0x000AAF89 File Offset: 0x000A9189
		internal static object AutoGeneratedGetMemberValueOwner(object o)
		{
			return ((CaravanPartyComponent)o).Owner;
		}

		// Token: 0x06002805 RID: 10245 RVA: 0x000AAF96 File Offset: 0x000A9196
		internal static object AutoGeneratedGetMemberValue_leader(object o)
		{
			return ((CaravanPartyComponent)o)._leader;
		}

		// Token: 0x17000A10 RID: 2576
		// (get) Token: 0x06002806 RID: 10246 RVA: 0x000AAFA3 File Offset: 0x000A91A3
		// (set) Token: 0x06002807 RID: 10247 RVA: 0x000AAFAB File Offset: 0x000A91AB
		[SaveableProperty(1)]
		public Settlement Settlement { get; private set; }

		// Token: 0x17000A11 RID: 2577
		// (get) Token: 0x06002808 RID: 10248 RVA: 0x000AAFB4 File Offset: 0x000A91B4
		// (set) Token: 0x06002809 RID: 10249 RVA: 0x000AAFBC File Offset: 0x000A91BC
		[SaveableProperty(2)]
		public Hero Owner { get; private set; }

		// Token: 0x17000A12 RID: 2578
		// (get) Token: 0x0600280A RID: 10250 RVA: 0x000AAFC5 File Offset: 0x000A91C5
		public override Hero PartyOwner
		{
			get
			{
				return this.Owner;
			}
		}

		// Token: 0x17000A13 RID: 2579
		// (get) Token: 0x0600280B RID: 10251 RVA: 0x000AAFD0 File Offset: 0x000A91D0
		public override TextObject Name
		{
			get
			{
				if (this._cachedName == null)
				{
					this._cachedName = GameTexts.FindText("str_caravan_party_name", null);
					TextObject cachedName = this._cachedName;
					string tag = "OWNER";
					Hero leaderHero = base.MobileParty.LeaderHero;
					cachedName.SetCharacterProperties(tag, ((leaderHero != null) ? leaderHero.CharacterObject : null) ?? this.Owner.CharacterObject, false);
				}
				return this._cachedName;
			}
		}

		// Token: 0x17000A14 RID: 2580
		// (get) Token: 0x0600280C RID: 10252 RVA: 0x000AB033 File Offset: 0x000A9233
		public override Settlement HomeSettlement
		{
			get
			{
				return this.Settlement;
			}
		}

		// Token: 0x17000A15 RID: 2581
		// (get) Token: 0x0600280D RID: 10253 RVA: 0x000AB03B File Offset: 0x000A923B
		public override Hero Leader
		{
			get
			{
				return this._leader;
			}
		}

		// Token: 0x0600280E RID: 10254 RVA: 0x000AB043 File Offset: 0x000A9243
		protected internal CaravanPartyComponent(Settlement settlement, Hero owner, Hero partyLeader)
		{
			this.Settlement = settlement;
			this.Owner = owner;
			this._leader = partyLeader;
		}

		// Token: 0x0600280F RID: 10255 RVA: 0x000AB060 File Offset: 0x000A9260
		protected override void OnInitialize()
		{
			this.Owner.OwnedCaravans.Add(this);
		}

		// Token: 0x06002810 RID: 10256 RVA: 0x000AB073 File Offset: 0x000A9273
		protected override void OnFinalize()
		{
			this.Owner.OwnedCaravans.Remove(this);
		}

		// Token: 0x06002811 RID: 10257 RVA: 0x000AB087 File Offset: 0x000A9287
		public override void ChangePartyLeader(Hero newLeader)
		{
			this._leader = newLeader;
		}

		// Token: 0x06002812 RID: 10258 RVA: 0x000AB090 File Offset: 0x000A9290
		public override void ClearCachedName()
		{
			this._cachedName = null;
		}

		// Token: 0x06002813 RID: 10259 RVA: 0x000AB09C File Offset: 0x000A929C
		public static MobileParty CreateCaravanParty(Hero caravanOwner, Settlement spawnSettlement, bool isInitialSpawn = false, Hero caravanLeader = null, ItemRoster caravanItems = null, int troopToBeGiven = 0, bool isElite = false)
		{
			MobileParty mobileParty2 = MobileParty.CreateParty("caravan_template_" + spawnSettlement.Culture.StringId.ToLower() + "_1", new CaravanPartyComponent(spawnSettlement, caravanOwner, caravanLeader), delegate(MobileParty mobileParty)
			{
				(mobileParty.PartyComponent as CaravanPartyComponent).InitializeCaravanOnCreation(mobileParty, caravanLeader, caravanItems, troopToBeGiven, isElite);
			});
			if (spawnSettlement.Party.MapEvent == null && spawnSettlement.SiegeEvent == null)
			{
				mobileParty2.Ai.SetMoveGoToSettlement(spawnSettlement);
				mobileParty2.Ai.RecalculateShortTermAi();
				EnterSettlementAction.ApplyForParty(mobileParty2, spawnSettlement);
			}
			else
			{
				mobileParty2.Ai.SetMoveModeHold();
			}
			if (mobileParty2.LeaderHero != null)
			{
				CampaignEventDispatcher.Instance.OnHeroGetsBusy(mobileParty2.LeaderHero, HeroGetsBusyReasons.BecomeCaravanLeader);
			}
			return mobileParty2;
		}

		// Token: 0x06002814 RID: 10260 RVA: 0x000AB168 File Offset: 0x000A9368
		private void InitializeCaravanOnCreation(MobileParty mobileParty, Hero caravanLeader, ItemRoster caravanItems, int troopToBeGiven, bool isElite)
		{
			this.InitializeCaravanProperties();
			if (troopToBeGiven == 0)
			{
				float num;
				if (MBRandom.RandomFloat < 0.67f)
				{
					num = (1f - MBRandom.RandomFloat * MBRandom.RandomFloat) * 0.5f + 0.5f;
				}
				else
				{
					num = 1f;
				}
				int num2 = (int)((float)mobileParty.Party.PartySizeLimit * num);
				if (num2 >= 10)
				{
					num2--;
				}
				troopToBeGiven = num2;
			}
			PartyTemplateObject pt = isElite ? this.Settlement.Culture.EliteCaravanPartyTemplate : this.Settlement.Culture.CaravanPartyTemplate;
			mobileParty.InitializeMobilePartyAtPosition(pt, this.Settlement.GatePosition, troopToBeGiven);
			if (caravanLeader != null)
			{
				mobileParty.MemberRoster.AddToCounts(caravanLeader.CharacterObject, 1, true, 0, 0, true, -1);
			}
			else
			{
				CharacterObject character2 = CharacterObject.All.First((CharacterObject character) => character.Occupation == Occupation.CaravanGuard && character.IsInfantry && character.Level == 26 && character.Culture == mobileParty.Party.Owner.Culture);
				mobileParty.MemberRoster.AddToCounts(character2, 1, true, 0, 0, true, -1);
			}
			mobileParty.ActualClan = this.Owner.Clan;
			mobileParty.Party.SetVisualAsDirty();
			mobileParty.InitializePartyTrade(10000 + ((this.Owner.Clan == Clan.PlayerClan) ? 5000 : 0));
			if (caravanItems != null)
			{
				mobileParty.ItemRoster.Add(caravanItems);
				return;
			}
			float num3 = 10000f;
			ItemObject itemObject = null;
			foreach (ItemObject itemObject2 in Items.All)
			{
				if (itemObject2.ItemCategory == DefaultItemCategories.PackAnimal && !itemObject2.NotMerchandise && (float)itemObject2.Value < num3)
				{
					itemObject = itemObject2;
					num3 = (float)itemObject2.Value;
				}
			}
			if (itemObject != null)
			{
				mobileParty.ItemRoster.Add(new ItemRosterElement(itemObject, (int)((float)mobileParty.MemberRoster.TotalManCount * 0.5f), null));
			}
		}

		// Token: 0x06002815 RID: 10261 RVA: 0x000AB390 File Offset: 0x000A9590
		private void InitializeCaravanProperties()
		{
			base.MobileParty.Aggressiveness = 0f;
		}

		// Token: 0x06002816 RID: 10262 RVA: 0x000AB3A4 File Offset: 0x000A95A4
		public override void GetMountAndHarnessVisualIdsForPartyIcon(PartyBase party, out string mountStringId, out string harnessStringId)
		{
			IFaction mapFaction = party.MapFaction;
			string text;
			if (mapFaction == null)
			{
				text = null;
			}
			else
			{
				CultureObject culture = mapFaction.Culture;
				text = ((culture != null) ? culture.StringId : null);
			}
			string a = text ?? string.Empty;
			if (a == "aserai" || a == "khuzait")
			{
				mountStringId = "camel";
				if (party.Index % 2 == 0)
				{
					harnessStringId = "camel_saddle_a";
					return;
				}
				harnessStringId = "camel_saddle_b";
				return;
			}
			else
			{
				mountStringId = "mule";
				int num = party.Index % 3;
				if (num == 0)
				{
					harnessStringId = "mule_load_a";
					return;
				}
				if (num != 1)
				{
					harnessStringId = "mule_load_c";
					return;
				}
				harnessStringId = "mule_load_b";
				return;
			}
		}

		// Token: 0x06002817 RID: 10263 RVA: 0x000AB448 File Offset: 0x000A9648
		public static void TransferCaravanOwnership(MobileParty caravan, Hero newOwner, Settlement homeSettlement)
		{
			CaravanPartyComponent caravanPartyComponent = new CaravanPartyComponent(homeSettlement, newOwner, caravan.LeaderHero);
			caravan.PartyComponent = caravanPartyComponent;
			caravanPartyComponent.InitializeCaravanProperties();
			caravan.Party.SetVisualAsDirty();
		}

		// Token: 0x04000C30 RID: 3120
		public const int DefaultCaravanPartyTradeInitialGold = 10000;

		// Token: 0x04000C33 RID: 3123
		[CachedData]
		private TextObject _cachedName;

		// Token: 0x04000C34 RID: 3124
		[SaveableField(3)]
		private Hero _leader;
	}
}
